package com.woyou.ui.activity.orderdetail;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.citaq.ideliver.R;
import com.woyou.bean.OrderDetail;
import com.woyou.bean.OrderStateLog;
import com.woyou.utils.DateUtils;

@SuppressLint("UseSparseArrays")
@SuppressWarnings("unused")
public abstract class OrderDetailAdapter {

	private HashMap<Integer, ArrayList<Integer>> nomorls;

	private ArrayList<ViewContentBean> list;
	private int state;
	private Context c;
	private OrderDetail detail;
	private ViewControl viewControl;

	public OrderDetailAdapter(Context c, ViewControl viewControl, OrderDetail orderDetail) {
		this.detail = orderDetail;
		this.viewControl = viewControl;
		nomorls = new HashMap<Integer, ArrayList<Integer>>();
		nomorls.put(State.NO_RESPONSE, new ArrayList<Integer>());
		nomorls.put(State.REFUSE, new ArrayList<Integer>());
		nomorls.put(State.CANCELLED, new ArrayList<Integer>());
		nomorls.put(State.PAID, new ArrayList<Integer>());

		ArrayList<Integer> a = new ArrayList<Integer>();
		a.add(2);
		// a.add(3);
		nomorls.put(State.CONFIRMED, a);

		a = new ArrayList<Integer>();
		a.add(2);
		// a.add(3);
		nomorls.put(State.SENT_OUT, a);

		a = new ArrayList<Integer>();
		a.add(2);
		nomorls.put(State.DELICERED, a);

		a = new ArrayList<Integer>();
		a.add(4);
		nomorls.put(State.COMMENTED, a);

		a = new ArrayList<Integer>();
		a.add(2);
		nomorls.put(State.AUTO_COMPLETE, a);

		a = new ArrayList<Integer>();
		a.add(2);
		nomorls.put(State.CANCELLATION, a);

		a = new ArrayList<Integer>();
		a.add(1);
		// a.add(3);
		nomorls.put(State.NEW_ORDER_ONLINEPAY, a);

		a = new ArrayList<Integer>();
		a.add(2);
		nomorls.put(State.CANCELLED_ONLINEPAY, a);
		list = getViewBeans(orderDetail);
		this.state = orderDetail.getState();
		this.c = c;
	}

	private ArrayList<ViewContentBean> getViewBeans(OrderDetail orderDetail) {
		if(orderDetail==null)
			return new ArrayList<ViewContentBean>();
		
		ArrayList<ViewContentBean> list = new ArrayList<ViewContentBean>();
		ArrayList<Integer> s = nomorls.get(orderDetail.getState());

		// 填充log数据
		ArrayList<OrderStateLog> logList = orderDetail.getOrderStateLog();
		if(logList!=null && logList.size()>0){
			for (int i = 0; i < logList.size(); i++) {
				ViewContentBean viewContentBean = new ViewContentBean();
				viewContentBean.setViewType(0);
				viewContentBean.setTEXT_name(orderDetail.getOrderStateLog().get(i).getLogName());
				viewContentBean.setTEXT_detail(orderDetail.getOrderStateLog().get(i).getLogDetails());
				viewContentBean.setTEXT_time(orderDetail.getOrderStateLog().get(i).getLogTime());
				list.add(viewContentBean);
			}
		}
		return list;
	}

	public int getCount() {
		return list.size();
	}

	public View getLog(int position) {
		View view = null;
		if (list.get(position).getViewType() == ViewContentBean.TEXT) {
			view = View.inflate(c, R.layout.orderdetail_item_text, null);
			TextView stateText = (TextView) view.findViewById(R.id.tx1);
			TextView detailText = (TextView) view.findViewById(R.id.dengdai);
			ViewContentBean item = list.get(position);
			if (item.getTEXT_detail().equals("")) {
				detailText.setVisibility(View.GONE);
			}
			TextView timeText = (TextView) view.findViewById(R.id.orderdetail_acceptorder_sendtime);
//			if (item.getTEXT_name().length() > 9) {
//				stateText.setText(item.getTEXT_name().substring(0, 8) + "..");
//			} else {
//				stateText.setText(item.getTEXT_name());
//			}
			stateText.setText(item.getTEXT_name());
			detailText.setText(item.getTEXT_detail());
			
			String nowDate = DateUtils.formatDate(item.getTEXT_time());
			String yestoryDate = DateUtils.getYestoryDate();
			String todayDate = DateUtils.getTodayDate();
			String str ="";
			if(yestoryDate.equals(nowDate)){
				str = "昨天 " + DateUtils.getTime(item.getTEXT_time());
			} else if(todayDate.equals(nowDate)){
				str = DateUtils.getTime(item.getTEXT_time());
			} else {
				str = nowDate.substring(5, 10) + " " + DateUtils.getTime(item.getTEXT_time());
			}
			timeText.setText(str);

		}
		// if (list.get(position).getViewType() == ViewContentBean.TEXT_EXTEND)
		// {
		// v = View.inflate(c, R.layout.orderdetail_item_text_extend, null);
		// ((TextView)
		// v.findViewById(R.id.price)).setText(list.get(position).getPrice());
		// }
		// if (list.get(position).getViewType() == ViewContentBean.ONE_BUTTON) {
		// v = View.inflate(c, R.layout.orderdetial_item_onebutton, null);
		// BindButtonClick((TextView) v.findViewById(R.id.button1), null, null,
		// state);
		// }
		// if (list.get(position).getViewType() == ViewContentBean.TOW_BUTTON) {
		// v = View.inflate(c, R.layout.orderdetial_item_towbutton, null);
		// BindButtonClick(null, (TextView) v.findViewById(R.id.button2),
		// (TextView) v.findViewById(R.id.button3),
		// state);
		// }
		// if (list.get(position).getViewType() == ViewContentBean.COMMENT) {
		// v = View.inflate(c, R.layout.orderdetial_item_comment, null);
		// // ((TextView)
		// //
		// v.findViewById(R.id.evaluation_assmenttime)).setText(viewContentBean.getComment().getcTime());
		// // ((TextView)
		// //
		// v.findViewById(R.id.evaluation_content)).setText(viewContentBean.getTEXT_name());

		return view;
	}

	public View[] getView() {
		return StateViewFactory.createStateView(c, viewControl, detail);
	}

}
